package com.zy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zy.entity.ZyMenu;
import com.zy.entity.ZyRole;
import com.zy.entity.ZyUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

@Repository
public interface ManageUserRoleMenuMappingMapper extends BaseMapper<ZyUser> {

    //添加user
    @Insert("insert into zy_user(username,password,nickname,phone,email)" +
            "values(#{username},#{password},#{nickname},#{phone},#{email})")
    void saveUser(ZyUser zyUser);

    //删除user
    @Delete("delete from zy_user where username = #{username}")
    void deleteUserByName(String username);

    //根据username查询user_id
    @Select("select user_id from zy_user where username = #{username}")
    Integer getUserIdByName(String username);

    @Select("select * from zy_user where username = #{userName}")
    ZyUser getUserByName(String userName);

    //添加user_role
    @Insert("insert into zy_user_role_mapping(user_id,role_id) values(#{userId},#{roleId})")
    void saveUserRole(Integer userId, Integer roleId);

    //删除user_role
    @Delete("delete from zy_user_role_mapping where user_id = #{userId}")
    void deleteUserRole(Integer userId);

    //修改zy_user
    @Update("update zy_user set password=#{password}, nickname=#{nickname}, phone=#{phone}, email=#{email} where username=#{username}")
    void updateUser(ZyUser zyUser);

    //修改zy_user_role_mapping
    @Update("update zy_user_role_mapping set user_id=#{userId}, role_id=#{roleId}")
    void updateRoleByUserId(Integer userId, Integer roleId);

    //删除zy_user_role_mapping
    @Delete("delete from zy_user_role_mapping where user_id=#{userId}")
    void deleteRoleByUserId(Integer userId);

    //查询所有role
    @Select("select * from zy_role")
    List<ZyRole> listRoles();

    //查询所有menu
    @Select("select * from zy_menu")
    List<ZyMenu> listMenus();

    //插入role
    @Insert("insert into zy_role(role_name,role_info) values(#{roleName},#{roleInfo})")
    void addRole(ZyRole zyRole);

    //根据role_name查询role_id
    @Select("select role_id from zy_role where role_name = #{roleName}")
    Integer getRoleIdByRoleName(String roleName);

    //插入role_menu
    @Insert("insert into zy_role_menu_mapping(role_id,menu_id) values(#{roleId},#{menuId})")
    void addRoleMenu(Integer roleId, Integer menuId);

    //根据roleId删除role
    @Delete("delete from zy_role where role_id = #{roleId}")
    void deleteRole(Integer roleId);

    //根据roleId删除role_menu
    @Delete("delete from zy_role_menu_mapping where role_id = #{roleId}")
    void deleteRoleMenu(Integer roleId);

    //根据roleId查询menuId
    @Select("select menu_id from zy_role_menu_mapping where role_id = #{roleId}")
    Integer[] getMenuIdByRoleId(Integer roleId);

    //根据roleId修改role
    @Update("update zy_role set role_name = #{roleName},role_info = #{roleInfo} where role_id = #{roleId}")
    void updateRoleByRoleId(ZyRole zyRole);

}
